package com.iot.controller;

import com.iot.dto.ConfigSetRequest;
import com.iot.entity.DeviceConfig;
import com.iot.relay.ExchangeData;
import com.iot.service.DeviceConfigService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "设备配置")
@RequestMapping("/config")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceConfigController {

    private final DeviceConfigService configService;

    @PostMapping("/set")
    public ExchangeData<Object> setConfig(@RequestBody ConfigSetRequest request) {
        ExchangeData<Object> exchangeData = new ExchangeData<>();
        configService.setConfig(request.getDeviceId(), request.getKey(), request.getValue());
        exchangeData.setMessage("设备注册成功");
        return exchangeData;
    }

    @GetMapping("/{deviceId}")
    public ExchangeData<List<DeviceConfig>> getConfigs(@PathVariable String deviceId) {
        ExchangeData<List<DeviceConfig>> exchangeData = new ExchangeData<>();
        exchangeData.setData(configService.getConfigByDevice(deviceId));
        return exchangeData;
    }

    @DeleteMapping("/{deviceId}/{key}")
    public ExchangeData<Object> deleteConfig(@PathVariable String deviceId, @PathVariable String key) {
        ExchangeData<Object> exchangeData = new ExchangeData<>();
        configService.deleteConfig(deviceId, key);
        exchangeData.setMessage("配置已删除");
        return exchangeData;
    }
}
